<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/13 0013
 * Time: 18:12
 * 基础控制器，主要用作一些自动加载类和方法
 */
namespace app\admin\controller;
use think\Controller;
use think\Request;

class Base extends Controller{
    public function _initialize(){
		 if(!cookie('login')) {
            // cookie不存在则跳转到登录页面
            $this->redirect('login/login');
        }
        if( Request::instance()->action() == ('index' || 'logout' || 'login' || 'main') ){
            return true;
		}
        $auth = new Auth();
        if ($auth->check(Request::instance()->module() . '/' . Request::instance()->controller() . '/' . Request::instance()->action(), 1)) {
            echo '你具备该权限';
        } else {
            echo '你不具备该权限';
        }
    }
    /**
     * ajax请求返回成功信息
     * @param  string $data
     * @return array
     */
    public static function formatSuccessResult($data = null){
        return self::formatResult(0, '操作成功', $data);
    }
    /**
     * ajax请求返回错误信息
     * @param int $code
     * @param string $errorMsg
     * @param string $data
     * @return array
     */
    public static function formatResult($code, $errorMsg, $data = null){
        return array('code' => $code,'msg' => $errorMsg,'data'=>$data);
    }
    /**
     * 验证手机号是否正确
     * @author honfei
     * @param number $mobile
     */
    function isMobile($mobile) {
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }
}
